/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "settings.h"
#include "constants.h"

#include <QSettings>
#include <QApplication>
#include <QDir>
#include <QMessageBox>

using namespace Constants;

settings::settings(QObject * parent) : QSettings(QDir::homePath()+"/.bmisurvey/config.ini",QSettings::IniFormat)
{
    Q_UNUSED(parent);
}

settings::~settings(){}

bool settings::isItFirstConfig()
{
    bool test = false;
    QString configfile = qApp->applicationDirPath()+"/../resources/config/config.ini";
    QString pointconfigfilepath = QDir::homePath()+"/.bmisurvey/config.ini";
    QString pointdir = QDir::homePath()+"/.bmisurvey";
    QDir pointbmisurvey(pointdir) ;
    if (!pointbmisurvey.exists())
    {
          QDir home = QDir::home();
          if (home.mkdir(".bmisurvey"))
          {
                QMessageBox::information(0,tr("Information"),tr("Le répertoire ./bmisurvey est créé."),QMessageBox::Ok);
                test = true;
              }           
        }
    
    if (!pointbmisurvey.entryList().contains("config.ini"))
    {
          if (createPointConfig())
          {
                QMessageBox::information(0,tr("Information"),"/.bmisurvey/config.ini"+tr(" a été créé."),QMessageBox::Ok);
              }
        } 
    return test;
}

bool settings::itIsFirstime()
{
    if (value(FIRSTTIME).toString()=="yes")
    {
          return true;
        }
    return false;
}


bool settings::createPointConfig()
{
    QString configfilepath = qApp->applicationDirPath()+"/../resources/config/config.ini";
    QString pointconfigfile = QDir::homePath()+"/.bmisurvey/config.ini";
    QFile configfile(configfilepath);
    return configfile.copy(pointconfigfile);    
}


QString settings::applicationPath() const 
{
#ifdef Q_OS_MAC
    return qApp->applicationDirPath() + "/../../../";
#else
    return qApp->applicationDirPath();
#endif
}

QString settings::resourcesPath() const  {
#ifdef Q_OS_MAC
    return qApp->applicationDirPath() + "/../../../../resources";
#else
    return qApp->applicationDirPath()+"/../resources";
#endif
}

QString settings::databasePath() const {
#ifdef Q_OS_MAC
    return qApp->applicationDirPath() + "/../../../../sqliteBase";
#else
    return qApp->applicationDirPath()+"/../sqliteBase";
#endif
}

QString settings::csvdatasfilePath() const 
{
#ifdef Q_OS_MAC
    return qApp->applicationDirPath() + "/../../../../resources/datasWH";
#else
    return qApp->applicationDirPath()+"/../resources/datasWH";
#endif
}

